#include "iBe.h"
#include <stdio.h>

ProcessorManagerPtr ProcessorManager::inst;

ProcessorManager::ProcessorManager()
{}

ProcessorManagerPtr ProcessorManager::instance()
{
  if ( !inst )
    {
      inst.reset( new ProcessorManager() );
    }
  return inst;
}

void ProcessorManager::setProcessor(int _proc)
{
  currProcessor = Processor::createProcessor( _proc );

  if (currProcessor && render)
    {
      SceneManagerPtr sceneManager = SceneManager::instance();
      ScenePtr scene = sceneManager->getBackScene();     
      currProcessor->init(scene);
      sceneManager->swapScene();
    }
}
 
void ProcessorManager::run(RenderPtr _render)
{
  bool done = false;
  int  i = 0, f = 1 ;
  render    = _render;
  char filename[100];

  WorldPtr world = World::instance();

  if (currProcessor)
    {
      return;
    }

  //setProcessor(Processor::STUB_PROC);
  setProcessor( Processor::TEST_PROC );

  while(!done && i < 30000)
    {
      sprintf(filename, "abc_%d.ppm", f);
      world->update(i);
      world->draw(render);
      std::cout << "writing.... " << filename << std::endl;
      render->writePPM(filename);
      i += 40;
      ++f;
    }
} 
